package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;


import at.ac.tuwien.ifs.calendit.basis.helper.Constants;
import at.ac.tuwien.ifs.calendit.domain.Raum;

import com.toedter.calendar.JDateChooser;


public final class Raumkalender extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable __table;
	private JScrollPane __scrollPane;
	private JPanel top;
    private JComboBox raum_cmb;
    private JButton ok_butt;
    public static JDateChooser cal;
    private TableCellRenderer defaultRenderer;
    private RaumTableModel rTM;
    private Date d;
    public static DefaultComboBoxModel drcbm;

	  public Raumkalender() {
	    super();
//		setRaumItems();
	    rTM= new RaumTableModel("ID");
	    List<Raum> raume= rTM.getRaum();
	    drcbm= new DefaultComboBoxModel();
	    for(int i=0; i< raume.size();i++){
			drcbm.addElement(""+raume.get(i).getRaumname());
		}
        __table = new JTable(new JRaumAnsichtTableModel());
	    defaultRenderer = __table.getDefaultRenderer(JComponent.class);
	    __table.setDefaultRenderer(JComponent.class,
				       new JComponentButtonRenderer(defaultRenderer));
	    TableColumn col = __table.getColumnModel().getColumn(0);
	    col.setPreferredWidth(80);
	    col.setMinWidth(80);
	    col.setMaxWidth(110); 
	    
	    //Register Listener
	    __table.setPreferredScrollableViewportSize(new Dimension(400, 200));
	    __table.addMouseListener(new JRaumMouseListener(__table));
	    __table.getTableHeader().setResizingAllowed(true);
	    __table.getTableHeader().setReorderingAllowed(false);
	    __table.setRowSelectionAllowed(false);
	    __table.setColumnSelectionAllowed(false);
	    __table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

	    __scrollPane = new JScrollPane(__table);
	    this.setLayout(new BorderLayout());
	    this.setBorder(BorderFactory.createTitledBorder("Raumkalender"));
	    this.setBackground(Color.white);
	    
	 // Add controls 
        top = new JPanel();
        top.setBackground(Color.WHITE);
       
        
        this.add(top, BorderLayout.NORTH);
        {
        	ok_butt = new JButton();
        	top.add(ok_butt);
        	ok_butt.setBorderPainted(false);
        	ok_butt.setBackground(Color.white);
        	ok_butt.setIcon(Constants.ICON_OK);
        	ok_butt.setPressedIcon(Constants.ICON_OK_pressed);
        	ok_butt.setPreferredSize(new Dimension(21,21));
        	ok_butt.addActionListener(new OKButton_Action2());
        }
        {
        	cal= new JDateChooser();
        	top.add(cal);
        	cal.setDate(Constants.selectedDate);
        }
        {
        	raum_cmb = new JComboBox();
        	raum_cmb.setModel(drcbm);
        	top.add(raum_cmb);
        	
        	raum_cmb.setPreferredSize(new java.awt.Dimension(116, 20));
        	raum_cmb.addActionListener(new ActionListener() {
        		public void actionPerformed(ActionEvent evt) {
        			raum_cmbActionPerformed(evt);
        		}
        	});
        }

	    this.add(__scrollPane,BorderLayout.CENTER);
	    this.repaint();

	  }
	  
	  private void raum_cmbActionPerformed(ActionEvent evt) {
		  d= new Date(cal.getDate().getYear(), cal.getDate().getMonth(), cal.getDate().getDate());
		  Constants.selectedDate=d; 
		  
		  JRaumAnsichtTableModel.jt.setText(Integer.toString(cal.getDate().getDay()));
		  JRaumAnsichtTableModel.refreshColumn();
		  
	  }
	  
	 private class OKButton_Action2 implements ActionListener{

			@SuppressWarnings("deprecation")
			public void actionPerformed(ActionEvent e) {

				 d= new Date(cal.getDate().getYear(), cal.getDate().getMonth(), cal.getDate().getDate());
				 Constants.selectedDate=d;
				 JRaumAnsichtTableModel.jt.setText(Integer.toString(cal.getDate().getDay()));
				 JRaumAnsichtTableModel.refreshColumn();

				 __table.getColumnModel().getColumn(1).setHeaderValue(JRaumAnsichtTableModel.weekdates[0].getDate()+"     "+"MO");
				 __table.getColumnModel().getColumn(2).setHeaderValue(JRaumAnsichtTableModel.weekdates[1].getDate()+"     "+"DI");
				 __table.getColumnModel().getColumn(3).setHeaderValue(JRaumAnsichtTableModel.weekdates[2].getDate()+"     "+"MI");
				 __table.getColumnModel().getColumn(4).setHeaderValue(JRaumAnsichtTableModel.weekdates[3].getDate()+"     "+"D0");
				 __table.getColumnModel().getColumn(5).setHeaderValue(JRaumAnsichtTableModel.weekdates[4].getDate()+"     "+"FR");
				 __table.getColumnModel().getColumn(6).setHeaderValue(JRaumAnsichtTableModel.weekdates[5].getDate()+"     "+"SA");
				 __table.getColumnModel().getColumn(7).setHeaderValue(JRaumAnsichtTableModel.weekdates[6].getDate()+"     "+"SO");
				    repaint();
				    
			}
	    	
	    }

	}
